<template>
  <div>
    <!-- 创建查询条件 -->
    <TopFilter
      ref="formPanel"
      :list="topFilterList"
      :cols="4"
      :collapse="true"
      @filterChange="changeHandle"
    ></TopFilter>
    <!-- 创建一个查询table -->
    <FilterTable
      ref="table"
      :fetchapi="fetchApi"
      :columns="columns"
      :params="fetchParams"
      :datakey="'rows'"
      :height=260
      :onColumnsChange="columns => (this.columns = columns)"
      :isSelectColumn="false"
      :isExportExcel="true"
    >
    <!--列操作-->
      <template slot="controls">
        <el-button size="small" type="primary"  @click="resetForm()">重置当前表</el-button>
      </template>
      <template slot="controls">
        <el-button size="small" type="primary" @click="submit()">提交</el-button>
      </template>
    </FilterTable>
    <FilterTable
      ref="tableB"
      :fetchapi="fetchApiB"
      :columns="columnsB"
      :params="fetchParamsB"
      :datakey="'rows'"
      :onColumnsChange="columnsB => (this.columnsB = columnsB)"
      :isSelectColumn="false"
      :isExportExcel="true"
    ></FilterTable>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import moment from 'moment';
import { getQuerySummary, getQueryStorage, resetForm, getQueryShywssget} from '@/api/repair/queryStatistics/queryBusinessSummary';

export default {
  mixins: [dictionary],
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createTopFilterList(),
      fetchApi: getQuerySummary,
      fetchApiB: getQueryStorage,
      fetchParams: {xhrAbort: true},
      fetchParamsB: {xhrAbort: true},
      columns: this.createColumns(),
      columnsB: this.createColumnsB(),
    };
  },
  created() {},
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.BaseTable = this.$refs.tableB.$refs.pageTable;
  },

  // 处理方法
  methods: {
    // 关闭页面触发事件
    // 创建查询条件
    createTopFilterList() {
      return [
        {
          type: 'DATE',
          dateType: 'month',
          label: '选择年月',
          fieldName: 'month',
          placeholder: '请选择年月',
          rules: [{ required: true, message:'请选择年月', month: true }],
          change: val => {
            this.changeDate(val);            
          }
        },
        {
          type: 'RANGE_DATE',
          label: '日期范围',
          style: { minWidth: '200px' },
          fieldName: 'minDate|maxDate',
          disabled: true
        }
      ];
    },
    createColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '委托书代码',
          dataIndex: 'REPAIR_TYPE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '具体维修类别',
          dataIndex: 'REPAIR_TYPE_DETAIL',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '维修客户类别',
          dataIndex: 'REPAIR_TYPE_CUS',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '台次',
          dataIndex: 'REPAIR_NUM',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '标准维修工时',
          dataIndex: 'STD_LABOUR_HOUR',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '实际维修工时',
          dataIndex: 'ASSIGN_LABOUR_HOUR',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '应收材料收入',
          dataIndex: 'SALE_PAR_SUM',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '实收材料收入',
          dataIndex: 'PAR_REAL_SUM',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '应收工时收入',
          dataIndex: 'RECEIVEABLE_LABOUR_FEE',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '实收工时收入',
          dataIndex: 'REAL_LABOUR_FEE',
          sorter: true,
          filter: true,
          filterType: 'number',
        },
        {
          title: '其他收入',
          dataIndex: 'OTHER_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '材料成本',
          dataIndex: 'REPAIR_PART_COST_SUM',
          sorter: true,
          filter: true,
          filterType: 'number'
        }
      ];
    },
    createColumnsB() {
      return [
        {
          title: '资产',
          dataIndex: 'CODE_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '期末数(元)',
          dataIndex: 'PART_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
      ];
    },
    changeDate(month){
      let minDate = moment(month).format('YYYY-MM-01');
      let maxDate = moment(month).add(1, 'month').subtract(1, 'day').format('YYYY-MM-DD');
      this.topFilterList.find(item => item.fieldName === 'minDate|maxDate').initialValue[0] = minDate;
      this.topFilterList.find(item => item.fieldName === 'minDate|maxDate').initialValue[1] = maxDate;
      this.topFilterList = [...this.topFilterList];
    },
    // 查询条件触发方法
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val, { offset: 0, xhrAbort: false });
      this.fetchParamsB = Object.assign({}, val, { offset: 0, xhrAbort: false });
    },
    // 所有用户
    async getAllUser() {
      const res = await getAllUser();
      let data = arrayObjDeWeight(res.data, 'userId'); // 人员去重(解决前端报错)
      this.columns.find(x => x.dataIndex === 'HFR').filterItems = this.columns.find(item => item.dataIndex === 'HFR').editItems = data.map(x => ({
        value: x.userId + '',
        text: x.employeeName
      }));
    },
    async getService() {
      const res = await getSeries();
      // this.topFilterList.find(x => x.fieldName === 'SERIES_NAME').itemList = res.data.map(x => ({ text: x.SERIES_CODE + '', value: x.SERIES_CODE + '' }));
      this.columns.find(x => x.dataIndex === 'SERIES_NAME').editItems = res.data.map(x => ({ text: x.SERIES_NAME, value: x.SERIES_CODE + '' }));
    },
    async submit(){
      let endDate=this.topFilterList.find(x => x.fieldName === 'month').initialValue;
      if(!endDate){
        return this.$notify.warning({ title: '提示信息', message: '请选择年月！' });
      }
      let param={endDate:endDate};
      const res = await getQueryShywssget(param);
      if (res.resultCode === 200) {
          this.$notify.success({ title: '提示信息', message: '提交成功！' });
          this.$emit('close', false);
      }
    },
    async resetForm(){
      let month = this.topFilterList.find(x => x.fieldName === 'month').initialValue;
      if(!month){
        return this.$notify.warning({ title: '提示信息', message: '请选择年月！' });
      }
      let res = await resetForm({month: month});
      if(res.data == '1'){
        this.$notify.success({ title: '提示信息', message: '重置成功！' });
        let month=this.topFilterList.find(x => x.fieldName === 'month').initialValue;
        this.changeHandle({month: month});
      }
    }
  }
};
</script>
